<template>
    <el-container>
        <el-aside :width=" isCollapse?'68px':'260px' ">
            <div class="logo-box">
                <img src="@/assets/logo.png" alt="" srcset="">
                <span>电商管理系统</span>
            </div>
            <el-menu mode="vertical"
                background-color="#545c64" 
                text-color="#fff" 
                active-text-color="#ffd04b"
                :default-active="$route.path"
                :unique-opened="false"
                :router="true"
                :collapse-transition="false"
                :collapse="isCollapse"
            >
                <el-submenu v-for="pm in meuns" :key="pm.id" :index="pm.id+''">
                    <template #title>
                        <i class="iconfont" :class="`icon-${pm.path}`"></i>
                        <span>{{ pm.authName }}</span>
                    </template>
                    <el-menu-item v-for="cm in pm.children" :key="cm.id" :index=" '/home/'+cm.path ">
                        <i class="el-icon-menu"></i>
                        <span>{{ cm.authName }}</span>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
            
        </el-aside>
        <el-container>
            <el-header>
                <el-button :icon=" isCollapse?'el-icon-s-unfold':'el-icon-s-fold' " 
                    @click="isCollapse=!isCollapse"
                ></el-button>
                <el-dropdown>
                    <div class="user-info">
                        <el-avatar size="large" shape="square" :src="require('@/assets/user.gif')"></el-avatar>
                        <span>{{ user.email }}<i class="el-icon-arrow-down el-icon--right"></i></span>
                    </div>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click.native="loginOut()">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <el-main>
                <el-breadcrumb separator-class="el-icon-arrow-right">
                    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item v-for="(name, index) in getBreads" :key="index">{{ name }}</el-breadcrumb-item>
                </el-breadcrumb>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            isCollapse:false,
            meuns:[],
            breadList:{},
            user:{}
        }
    },
    computed:{
        getBreads(){
            return this.breadList[this.$route.path]
        }
    },
    methods:{
        getUserInfo(){
            let usereStr = localStorage.getItem("user");
            this.user = JSON.parse(usereStr);
        },
        loginOut(){
            localStorage.removeItem("user");
            localStorage.removeItem("token");
            this.$router.push("/login");
        },
        async initLeftMenus(){
            let meuns = await this.$api.loadNavMenus()
            // console.log(meuns);
            let breadList={};
            meuns.forEach(pm => {
                pm.children.forEach(cm=>{
                    // console.log(pm.authName+":"+cm.authName);
                    // console.log("/home/"+cm.path);
                    let tempArr = [];
                    if(pm.authName!="首页"){ 
                        tempArr.push(pm.authName);
                    }
                    tempArr.push(cm.authName);
                    breadList["/home/"+cm.path] = tempArr;
                })
            });
            // console.log(breadList);
            this.meuns = meuns;
            this.breadList = breadList;
        }
    },
    created(){
        this.getUserInfo();
        this.initLeftMenus();
    }
}
</script>

<style lang="less" scoped>
.el-container{
    height: 100%;
    background-color: @border-lighter;
}
@h:60px;
.el-aside{
    background-color: @nav-menu;
    transition: all 0.6s ease;
    overflow: hidden;
    .logo-box{
        height: @h;
        width: 100%;
        border-bottom: 1px solid @border-lighter;
        box-sizing: border-box;
        white-space: nowrap;
        overflow: hidden;
        img,span{
            padding: 6px 10px;
            height: 100%;
            box-sizing: border-box;
            vertical-align: middle;
            color: white;
        }
        span{
            padding-left: 0px;
            font-size: 24px;
        }
    }
    .el-menu{
        border:none;
    }
    // .el-submenu [class="icon-"]{
    //     vertical-align: middle;
    //     margin-right: 5px;
    //     width: 24px;
    //     text-align: center;
    //     font-size: 18px;
    // }
    .el-submenu .iconfont {
        vertical-align: middle;
        margin-right: 5px;
        width: 24px;
        text-align: center;
        font-size: 17px;
    }
}
.el-header{
    background-color: @white;
    // box-shadow: 0px 0px 8px @text-primary;
    height: @h!important;
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: space-between;
    .el-button{
        font-size: 30px;
        padding: 4px 10px;
    }
    .user-info > span{
        cursor: pointer;
        vertical-align: middle;
        &:last-child{
            padding-left: 10px;
        }
    }
}
.el-main{
    box-shadow: 0px 0px 8px @text-primary inset;
}
.el-breadcrumb{
    padding: 20px 0px 30px 0px;
}
</style>